package com.baidu.service.impl;

import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbGoods;
import com.baidu.service.TbGoodsService;
import com.baidu.mapper.TbGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 *
 */
@Service
public class TbGoodsServiceImpl extends ServiceImpl<TbGoodsMapper, TbGoods>
    implements TbGoodsService{

    @Autowired
    private TbGoodsMapper goodsMapper;

    @Override
    public ResultResponse getGoodsPage(Integer current, Integer size, String goodsName, Integer status) {
        Page<TbGoods> page = new Page<>(current, size);
        LambdaQueryWrapper<TbGoods> lambda = new QueryWrapper<TbGoods>().lambda();
        if(!StringUtils.isEmpty(goodsName)){
            lambda.like(TbGoods::getGoodsName,goodsName);
        }
        if(status!=null){
            lambda.eq(TbGoods::getAuditStatus,status);
        }
        Page<TbGoods> tbGoodsPage = goodsMapper.selectPage(page, lambda);
        return ResultResponse.SUCCESS(tbGoodsPage);
    }

    @Override
    public ResultResponse delGoodsByIds(String ids) {
        if(StringUtils.isEmpty(ids)){
           return ResultResponse.FAILED(-1,"用选择ids");
        }
        String[] split = ids.split(",");
        for (String s : split) {
            goodsMapper.deleteById(Integer.parseInt(s));
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse commitAudit(Integer id) {
        UpdateWrapper<TbGoods> updateWrapper = new UpdateWrapper<TbGoods>().eq("id", id);

        TbGoods tbGoods = new TbGoods();

        tbGoods.setAuditStatus("1");

        goodsMapper.update(tbGoods,updateWrapper);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse shieldGoodsById(Integer id) {
        UpdateWrapper<TbGoods> updateWrapper = new UpdateWrapper<TbGoods>().eq("id", id);

        TbGoods tbGoods = new TbGoods();

        tbGoods.setIsMarketable("0");

        goodsMapper.update(tbGoods,updateWrapper);

        return ResultResponse.SUCCESS();

    }
}




